// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: hyuan

#pragma once

#include <cstdio>
#include <memory>
#include <set>
#include <string>

#include <rapidjson/document.h>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/phemex_base/api_util/rate_tracker.h"
#include "coin2/exchange/phemex_futures/symbology/product.h"

namespace coin2::exchange::phemex_futures::order_v1 {

class PhemexFuturesParser : public base::order::BaseParser {
 public:
  using RateLimitInfo = coin2::exchange::phemex::api_util::RateLimitInfo;

 public:
  const ParseResult& ParseRawMessage(const RawMessage& msg) override;
  AccountBalance ParseBalance(const JsonValue& doc) override;
  AccountBalance ParseHedgedBalance(const JsonValue& doc);
  AccountPosition ParsePosition(const JsonValue& doc) override;
  AccountPosition ParseHedgedPosition(const JsonValue& doc);

 private:
  void ParseRestAccountInfo();
  void ParseHedgedRestAccountInfo();
  bool ParseRawPosition(const JsonValue& positions, RawPositionMap* raw_position_map);
  void ParseRestOpenOrders();
  void ParseHedgedRestOpenOrders();
  void ParseRestHistoryOrders();
  void ParseHedgedRestHistoryOrders();
  void ParseRestFillOrders();
  void ParseHedgedRestFillOrders();
  void ParseRestTransfer();
  void ParseSubmitResponse();
  void ParseCancelResponse();
  void ParseWsOrderInfoList(const JsonDocument& document);
  void ParseHedgedWsOrderInfoList(const JsonDocument& document);
  void ParseWsFillInfoList(const JsonDocument& document);
  void ParseHedgedWsFillInfoList(const JsonDocument& document);
  void ParseWsAccountInfoList(const JsonDocument& document);
  void ParseHedgedWsAccountInfoList(const JsonDocument& document);
  void ParseWsPositionInfoList(const JsonDocument& document);
  void ParseHedgedWsPositionInfoList(const JsonDocument& document);
  void ParseWsMessage();

  void ParseHeader(const std::shared_ptr<AsioHttpContext>&);

 private:
  RateLimitInfo rate_limit_contract_{500};
  RateLimitInfo enable_time_contract_{0};
  RateLimitInfo rate_limit_others_{100};
  RateLimitInfo enable_time_others_{0};
};

}  // namespace coin2::exchange::phemex_futures::order_v1
